package com.jndv.phoneconfig.fragment;

import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jndv.phoneconfig.PublicConstant;
import com.jndv.phoneconfig.R;
import com.jndv.phoneconfig.command.AutoTimeStateCommand;
import com.kyleduo.switchbutton.SwitchButton;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeFragment extends BaseFragment implements View.OnClickListener {

    TimePickerView pvCustomTime;
    TextView tvEndTime;
    ImageView ivRight;
    SwitchButton sbAutoTime;
    RelativeLayout rlSetTime;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_time;
    }

    @Override
    protected void initView(View view) {
        ivRight=view.findViewById(R.id.iv_right);
        rlSetTime=view.findViewById(R.id.rl_set_time);
        rlSetTime.setOnClickListener(this);
        tvEndTime =view.findViewById(R.id.tv_end_time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        tvEndTime.setText(format.format(new Date()));

        initCustomTimePicker();

        sbAutoTime=view.findViewById(R.id.sb_auto_time);
        sbAutoTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sendMessage(PublicConstant.AUTO_TIME,isChecked?"1":"0");
                if(isChecked){
                    ivRight.setVisibility(View.GONE);
                    rlSetTime.setClickable(false);
                }else {
                    ivRight.setVisibility(View.VISIBLE);
                    rlSetTime.setClickable(true);
                }
            }
        });

        if(isSocketConnect()){
            sendMessage(PublicConstant.CUR_AUTO_TIME_STATE,"");
        }else {
            sbAutoTime.setChecked(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurAutoTimeStateEvent(AutoTimeStateCommand event) {
        sbAutoTime.setChecked(event.isAutoTime);
        if(event.isAutoTime){
            ivRight.setVisibility(View.GONE);
            rlSetTime.setClickable(false);
        }else {
            ivRight.setVisibility(View.VISIBLE);
            rlSetTime.setClickable(true);
        }
    }

    long timeStamp;

    private void initCustomTimePicker() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2014, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2050, 3, 31);
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                //选中事件回调
                timeStamp=date.getTime();
                Log.d("zhang","timeStamp=="+timeStamp);
                tvEndTime.setText(getTime(date));
            }
        }).setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(1.2f)
                .setTextXOffset(0, 0, 0, 40, 0, -40)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFF24AD9D)
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.rl_set_time){
            pvCustomTime.show();
        }
    }
}
